package jspSamples.unit7.websiteSample;

import java.sql.*;

public  class DBConnection {
    private static final   String MysqlURL="jdbc:mysql://localhost:3306/studb?serverTimezone=Asia/Shanghai";
    private static final   String Username="root";
    private static final   String Password="123456";

    {
        // 静态代码块 当访问改类的时候会自动执行静态代码块里的东西
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    // 访问getConn 返回 Connection 连接对象
    public static Connection getConn(){
        Connection  conn = null;
        try {
            conn = DriverManager.getConnection(MysqlURL, Username, Password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 关闭  结果集ResultSet  statement  connection
    public static void close(ResultSet resultSet, Statement statement, Connection conn){
        try {
            // 遵循先开后关的规则
            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // update
    /*  */
    public static void close( Statement statement, Connection conn){
        try {
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
